package pzl.com.library.chart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;

import pzl.com.library.data.BaseData;
import pzl.com.library.data.DataList;
import pzl.com.library.listener.ChartTouchListener;
import pzl.com.library.renderer.BaseAxisRenderer;
import pzl.com.library.renderer.BaseDataRenderer;
import pzl.com.library.utils.Util;

/**
 * @author zl.peng
 * @version [1.0, 2016-11-22]
 */
public abstract class BaseChart extends View implements IChart {
    private static final String TAG = "BaseChart";
    private final int default_width = 700;
    private final int default_height = 700;

    /**
     * 滚动可交互的类型：TouchMove、fling
     */
    protected int mScrollType;

    public static final int SCROLL_TYPE_ENABLE_X = 1;//仅x方向可滚动
    public static final int SCROLL_TYPE_ENABLE_Y = 2;//仅y方向可滚动
    public static final int SCROLL_TYPE_BOTH_ENABLE = 3;//所有方向都可滚动
    public static final int SCROLL_TYPE_BOTH_UNABLE = 0;//所有方向都不可滚动

    /**
     * 绘制时debug
     */
    public boolean debugDraw;
    private Paint mDebugPaint;
    private Rect mDebugDataAreaRect;
    /**
     * 数据绘制区域的高度
     */
    protected int mDataDrawAreaHeight;
    /**
     * 数据绘制区域的宽度
     */
    protected int mDataDrawAreaWidth;

    /**
     * view高度
     */
    protected int mViewHeight;
    /**
     * view宽度
     */
    protected int mViewWidth;

    /**
     * 基本数据渲染器
     */
    private BaseDataRenderer mBaseDataRenderer;
    /**
     * 数据
     */
    protected BaseData mBaseData;
    /**
     * 图表监听
     */
    private ChartTouchListener mChartTouchListener;
    /**
     * 坐标轴渲染器
     */
    protected ArrayList<BaseAxisRenderer> mAxisRenderers;


    private Context mContext;

    public BaseChart(Context context) {
        this(context, null);
    }

    public BaseChart(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BaseChart(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //default getDataListSize
        //setMeasuredDimension(default_width, default_height);
        mViewHeight = getMeasuredHeight();
        mViewWidth = getMeasuredWidth();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Log.d(TAG, "BaseChart drawData");
        super.onDraw(canvas);

        //绘制数据
        if (mBaseDataRenderer != null) mBaseDataRenderer.drawData(canvas, this);
        //绘制所有轴
        if (mAxisRenderers != null && mAxisRenderers.size() > 0) {
            for (BaseAxisRenderer axisRenderer : mAxisRenderers) {
                axisRenderer.drawAxis(canvas, this);
            }
        }

        //debug绘制
        if (debugDraw) {
            canvas.drawRect(mDebugDataAreaRect, mDebugPaint);
        }
    }


    @Override
    public BaseDataRenderer getRenderer() {
        return mBaseDataRenderer;
    }

    @Override
    public void setRenderer(BaseDataRenderer renderer) {
        mBaseDataRenderer = renderer;
    }

    @Override
    public BaseData getData() {
        return mBaseData;
    }

    @Override
    public void setData(BaseData data) {
        mBaseData = data;
    }

    @Override
    public void init() {
        Log.d(TAG, "BaseChart init");
        mBaseData = onCreateBaseData();
        mChartTouchListener = onCreateChartTouchListener();
        mBaseDataRenderer = onCreateBaseRenderer();
        mAxisRenderers = onCreateAxisRenderer();

        //debug init
        mDebugPaint = new Paint();
        mDebugPaint.setAntiAlias(true);
        mDebugPaint.setStyle(Paint.Style.STROKE);
        mDebugPaint.setStrokeWidth(2);
        mDebugPaint.setColor(Color.RED);
        mDebugDataAreaRect = new Rect();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mChartTouchListener.onTouch(this, event);
    }

    public void addDataSet(String key, DataList dataList) {
        if (mBaseData == null) {
            Log.e(TAG, "baseData is null");
            return;
        }
        mBaseData.addDataList(key, dataList);
    }

    /**
     * 初始化事件处理的监听器
     *
     * @return
     */
    abstract ChartTouchListener onCreateChartTouchListener();

    /**
     * 初始化数据
     *
     * @return
     */
    abstract BaseData onCreateBaseData();

    /**
     * 初始化渲染器
     *
     * @return
     */
    abstract BaseDataRenderer onCreateBaseRenderer();

    /**
     * 初始化坐标轴渲染器
     *
     * @return
     */
    abstract ArrayList<BaseAxisRenderer> onCreateAxisRenderer();


    @Override
    public void computeScroll() {
        if (mChartTouchListener != null) mChartTouchListener.computeScroll();
    }

    /**
     * @param dpWidth
     */
    public void setDataDrawAreaWidth(int dpWidth) {
        mDataDrawAreaWidth = Util.dp2px(dpWidth, mContext);
    }

    /**
     * @param dpHeight
     */
    public void setDataDrawAreaHeight(int dpHeight) {
        mDataDrawAreaHeight = Util.dp2px(dpHeight, mContext);
    }

    /**
     * @param scrollType
     */
    public void setScrollType(int scrollType) {
        mScrollType = scrollType;
    }

    public int getScrollType() {
        return mScrollType;
    }

    /**
     * 当前滚动的总量（包括 move 和 动画fling的）
     *
     * @param sumScrollX
     * @param sumScrollY
     */
    public void onScroll(float sumScrollX, float sumScrollY) {
        //刷新debug绘制数据
        mDebugDataAreaRect.top = mViewHeight / 2 - mDataDrawAreaHeight / 2 + (int) sumScrollY;
        mDebugDataAreaRect.bottom = mDebugDataAreaRect.top + mDataDrawAreaHeight;
        mDebugDataAreaRect.left = mViewWidth / 2 - mDataDrawAreaWidth / 2 + (int) sumScrollX;
        mDebugDataAreaRect.right = mDebugDataAreaRect.left + mDataDrawAreaWidth;


    }


}
